<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\omsRuntime;

use think\Model;

class LogBusinessException extends Model {

    protected $base_table = 'log_business_exception';
    protected $table = 'log_business_exception';
    protected $connection = ['database' => 'omsRuntime'];

    public function __construct($company_id = []) {

        if (!is_array($company_id)) {
            $this->table = $this->base_table . '_' . $company_id;
        }

        parent::__construct($company_id);
    }
    
    private function _createTable() {
        $sql = 'CREATE TABLE ' . $this->table . ' LIKE ' . $this->_baseTable;
        return $this->execute($sql);
    }
    
    public function addLog($params=[]) {
        
        $data = [
            'action' => isset($params['action'])?$params['action']:'',
            'type' => isset($params['type'])?$params['type']:'',
            'message' => isset($params['message'])?$params['message']:'',
            'ts' => time(),
        ];

        try {
            $this->save($data);
        } catch (\Exception $e) {
            if ($e->errorInfo[1] == '1146') {
                if($this->_createTable()){
                    $this->addLog($params);
                }
            }
        }
    }
}
